#include <string.h>
#include <iostream>
using std::cout;
using std::endl;

class Computer{
public:
    Computer(const char * brand, float price)
    :_brand(new char[strlen(brand) + 1]())
    ,_price(price)
    {
        strcpy(_brand ,brand);
    }

    Computer &operator=(const Computer &rf){
        delete []_brand;
        _brand = new char[strlen(rf._brand)+1]();
        strcpy(_brand,rf._brand);
        _price = rf._price; 
        return *this;
    }
    ~Computer(){
        if(_brand){
            delete [] _brand;
            _brand = nullptr;
        }
    }

    //赋值运算符函数--编译器默认会提供
    //又是因为默认执行了浅拷贝，导致了double free

    void print(){
        cout << "_brand=" << _brand << ",_price=" << _price << endl;
    }


private:
    char * _brand;
    float _price;
};
void test(){
    Computer c1("Apple", 12000), c2("Huawei", 7800);
    c1 = c2;
    c1.print();
    c2.print();
}

int main()
{
    test();
    return 0;
}


